package org.habitatguate.hgerp.seguridad.client;

import org.habitatguate.hgerp.seguridad.client.api.LoginService;
import org.habitatguate.hgerp.seguridad.client.api.LoginServiceAsync;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.FlexTable.FlexCellFormatter;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.StackLayoutPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style.Unit;

public class Index implements EntryPoint {

	private final LoginServiceAsync loginService = GWT
			.create(LoginService.class);
	
	@Override
	public void onModuleLoad() {
		// TODO Auto-generated method stub
		// Create a table to layout the form options
	    FlexTable layout = new FlexTable();
	    layout.setCellSpacing(6);
	    FlexCellFormatter cellFormatter = layout.getFlexCellFormatter();

	    // Add a title to the form
	    layout.setHTML(0, 0, "Login");
	    cellFormatter.setColSpan(0, 0, 2);
	    cellFormatter.setHorizontalAlignment(
	        0, 0, HasHorizontalAlignment.ALIGN_CENTER);
	    
	    final TextBox user =new TextBox();
	    final TextBox pass =new TextBox();

	    // Add some standard form options
	    layout.setHTML(1, 0, "Usuario:");
	    layout.setWidget(1, 1,user );
	    layout.setHTML(2, 0, "Password:");
	    layout.setWidget(2, 1, pass);

	    final Button sendButton = new Button("Send");
	    
	    final DialogBox dialogBox = new DialogBox();
		dialogBox.setText("Autenticacion");
		dialogBox.setAnimationEnabled(true);
		final Button closeButton = new Button("X");
		// We can set the id of a widget by accessing its Element
		closeButton.getElement().setId("closeButton");
		final Label textToServerLabel = new Label();
		final HTML serverResponseLabel = new HTML();
		VerticalPanel dialogVPanel = new VerticalPanel();
		dialogVPanel.addStyleName("dialogVPanel");
		dialogVPanel.add(new HTML("<b>Sending name to the server:</b>"));
		dialogVPanel.add(textToServerLabel);
		dialogVPanel.add(new HTML("<br><b>Server replies:</b>"));
		dialogVPanel.add(serverResponseLabel);
		dialogVPanel.setHorizontalAlignment(VerticalPanel.ALIGN_RIGHT);
		dialogVPanel.add(closeButton);
		dialogBox.setWidget(dialogVPanel);
        
		// Add a handler to close the DialogBox
		closeButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				dialogBox.hide();
				sendButton.setEnabled(true);
				sendButton.setFocus(true);
			}
		});
		
		
		// Create a handler for the sendButton and nameField
				class MyHandler implements ClickHandler{
					/**
					 * Fired when the user clicks on the sendButton.
					 */
					public void onClick(ClickEvent event) {
						login();
					}



					/**
					 * Send the name from the nameField to the server and wait for a response.
					 */
					private void login() {
						// First, we validate the input.
						String usertxt = user.getText();
						String passtxt= pass.getText();


						// Then, we send the input to the server.
						sendButton.setEnabled(false);
						textToServerLabel.setText(usertxt+" / "+passtxt);
						serverResponseLabel.setText("");
						loginService.login(usertxt,passtxt,
								new AsyncCallback<String[]>() {
									public void onFailure(Throwable caught) {
										// Show the RPC error message to the user
										dialogBox
												.setText("Error en login.");
										dialogBox.center();
										closeButton.setFocus(true);
									}

									public void onSuccess(String result[]) {
										if(result!=null){
											RootPanel.get().clear();
											RootPanel.get().add(buildMenu(result));
										}else{
											dialogBox
											.setText("Usuario o password incorrecto");
									dialogBox.center();
									closeButton.setFocus(true);
										}
										
									}
								});
					}
				}

				// Add a handler to send the name to the server
				MyHandler handler = new MyHandler();
				sendButton.addClickHandler(handler);
	    
				layout.setWidget(3, 0, sendButton);
				//layout.setWidget(4, 1, dialogBox);
	    // Wrap the content in a DecoratorPanel
	    DecoratorPanel decPanel = new DecoratorPanel();
	    decPanel.setWidget(layout);	    
	    RootPanel.get().add(decPanel);
	    //RootPanel.get().add(dialogBox);
	    

	}
	
	private Widget buildMenu(String menus[]){
		
		StackLayoutPanel stackPanel = new StackLayoutPanel(Unit.EM);
	    stackPanel.setPixelSize(300, 500);
	    
	    int size=menus.length;
	    for(int i=0;i<size;i++){
	    	stackPanel.add(createItem(menus[i]),createHeaderWidget(menus[i]),4);
	    }


	    // Return the stack panel.
	    stackPanel.ensureDebugId("cwStackLayoutPanel");
	    return stackPanel;
	}
	
	private Widget createItem(String item) {
	    
	    return new SimplePanel(getOpciones());
	    
	  }
	
	public Widget getOpciones() {
	    // Add a label
	    VerticalPanel vPanel = new VerticalPanel();
	    vPanel.setSpacing(5);
	    
	    Hyperlink link = new Hyperlink("OPCION 1", "OLD");	 
	    vPanel.add(link);
	    
	    Hyperlink link2 = new Hyperlink("OPCION 2", "OLD");	 
	    vPanel.add(link2);
	    Hyperlink link3 = new Hyperlink("OPCION 3", "OLD");	 
	    vPanel.add(link3);
	    Hyperlink link4 = new Hyperlink("OPCION 4", "OLD");	 
	    vPanel.add(link4);

	    // Return the panel
	    return vPanel;
	  }
	
	private Widget createHeaderWidget(String text) {
	    // Add the image and text to a horizontal panel
	    HorizontalPanel hPanel = new HorizontalPanel();
	    hPanel.setHeight("100%");
	    hPanel.setSpacing(0);
	    hPanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
	    HTML headerText = new HTML(text);
	    hPanel.add(headerText);
	    return new SimplePanel(hPanel);
	  }
	

}
